Ext.ns("FileAttachView");
FileAttachView = Ext.extend(Ext.Panel,{
					constructor : function(a) {
						Ext.applyIf(this, a);
						this.initUIComponents();
						FileAttachView.superclass.constructor.call(this, {
							id : "FileAttachView",
							height : 450,
							autoScroll : true,
							layout : "border",
							title : "文件列表",
							iconCls : "menu-dictionary",
							items : [ this.searchPanel, this.leftPanel, this.centerPanel ]
						});
					},
					initUIComponents : function() {

						this.searchPanel = new Ext.FormPanel({
							region : "north",
							height : 35,
							frame : false,
							border : false,
							id : "fileDetailSearchForm",
							layout : "hbox",
							layoutConfig : {
								padding : "5",
								align : "middle"
							},
							defaults : {
								xtype : "label",
								border : false,
								margins : {
									top : 0,
									right : 4,
									bottom : 4,
									left : 4
								}
							},
							items : [ {
								text : "文件名"
							}, {
								xtype : "textfield",
								name : "Q_fileName_S_LK"
							}, {
								text : "上传时间  从"
							}, {
								xtype : "datefield",
								name : "Q_createtime_D_GT",
								format : "Y-m-d",
								width:120
							}, {
								text : "至"
							}, {
								xtype : "datefield",
								name : "Q_createtime_D_LT",
								format : "Y-m-d",
								width:120
							},{
								xtype : "button",
								text : "查询",
								iconCls : "search",
								scope : this,
								handler : this.onSearch.createCallback(this)
							}, {
								xtype : "button",
								text : "清空",
								scope : this,
								iconCls : "reset",
								handler : FileDetailSearchReset
							} ]
						});
						
						this.leftPanel = new zhixiang.ux.TreePanelEditor({
							region : "west",
							id : "forder",
							title : "文件夹",
							collapsible : true,
							split : true,
							height : 450,
							width : 160,
							autoScroll : true,
							url : __ctxPath + "/system/getAllForder.do",
							onclick : function(f) {
								this.selectedNode = f;
								var g = f.id;
								var e = Ext.getCmp("fileDetailGrid");
								if (e != null) {
									if (g == 0) {
										e.setTitle("所有文件");
									} else {
										e.setTitle(f.text + "-文件夹");
									}
									var d = e.getStore();
									d.proxy = new Ext.data.HttpProxy({
										url:__ctxPath+ "/system/listFileDetail.do?Q_forder.id_L_EQ="+g
									});
									d.baseParams = {
										parentId : g
									};
									d.reload();
								}
							},
							contextMenuItems : [
									{
										text : "新建文件夹",
										scope : this,
										iconCls : "btn-add",
										handler : function() {
											var e = Ext.getCmp("forder");
											var d = e.selectedNode.id;
											var f = new ForderForm({
												forId : null,
												parentId : d,
												callback : function() {
													Ext.getCmp("forder").root.reload();
												}
											}).show();
										}
									},
									{
										text : "修改文件夹",
										scope : this,
										iconCls : "btn-edit",
										handler : function() {
											var e = Ext.getCmp("forder");
											var d = e.selectedNode.id;
											var f = new ForderForm({
												forId : d,
												parentId : null,
												callback : function() {
													Ext.getCmp("forder").root
															.reload();
												}
											}).show();
										}
									},{
										text : "删除文件夹",
										scope : this,
										iconCls : "btn-delete",
										handler : function() {
											var e = Ext.getCmp("forder");
											var d = e.selectedNode.id;
											if(d==1){
												Ext.ux.Toast.msg("操作提示", "根目录不能删除!");
												return;
											}
											Ext.Msg.confirm("删除操作", "你确定删除文件夹?", function(p) {
												if (p == "yes") {
													Ext.Ajax.request({
														url : __ctxPath + "/system/deleteForder.do?ids="+d,
														success : function(q, s) {
															var r = Ext.util.JSON.decode(q.responseText);
															if (r.success == false) {
																Ext.MessageBox.show({
																	title : "操作提示",
																	msg : r.msg,
																	buttons : Ext.MessageBox.OK,
																	icon : "ext-mb-error"
																});								
															} else {
																Ext.ux.Toast.msg("操作提示", "删除成功!");
															}
															Ext.getCmp("forder").root.reload();
														},
														failure : function(q, r) {
														}
													});
												}
											});
										}
									} ]
						});
						
						this.store = new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : __ctxPath + "/system/listFileDetail.do"
							}),
							reader : new Ext.data.JsonReader({
								root : "result",
								totalProperty : "totalCounts",
								id : "id",
								fields : [ {
									name : "fileId",
									type : "int"
								}, "fileName", "filePath", "createtime", "delFlag", "ext", "type",
										"note", "creator", "fileType","fileSize"]
							}),
							remoteSort : true
						});
						this.store.setDefaultSort("fileId", "desc");
						this.store.load({
							params : {
								start : 0,
								limit : 25
							}
						});
						var c = new Ext.grid.CheckboxSelectionModel();
						var a = new Ext.grid.ColumnModel({
							columns : [c,
										{
											header : "fileId",
											dataIndex : "fileId",
											hidden : true
										},
										{
											header : "文件名",
											dataIndex : "fileName"
										},
										{
											header : "文件路径",
											dataIndex : "filePath",
											width : 350
										},
										{
											header : "文件大小",
											dataIndex : "fileSize",
											width : 100
										},
										{
											header : "上传时间",
											dataIndex : "createtime",
											width : 200
										},
										/*{
											header : "扩展名",
											dataIndex : "ext"
										},
										{
											header : "文件类型",
											dataIndex : "fileType"
										},
										{
											header : "说明",
											dataIndex : "note"
										},*/
										{
											header : "上传者",
											dataIndex : "creator",
											width : 100
										},
										/*{
											header : "状态",
											dataIndex : "delFlag",
											renderer : function(e) {
												if (e) {
													if (e == 1) {
														return '<font color="red">已删除</font>';
													} else {
														return '<font color="green">可用</font>';
													}
												} else {
													return "";
												}
											}
										},*/
										{
											header : "查看与下载",
											dataIndex : "fileId",
											width : 80,
											renderer : function(h, g, e, k, f) {
												var j = e.data.fileId;
												var i = "";
												if (isGranted("_FileAttachShow")) {
													i += '&nbsp;<button title="查看" value=" " class="btn-showDetail" onclick="FileAttachDetail.show1('
															+ j + ')"></button>';
												}
												if (isGranted("_FileAttachDown")) {
                                                    i += '&nbsp;　<button title="下载" value=" " class="btn-detail" onclick="FileAttachDetail.downLoad('
                                                            + j + ')"></button>';
                                                }
												return i;
											}
										} ],
								defaults : {
									sortable : true,
									menuDisabled : true,
									width : 300
								}
							});
							 var b = new Ext.Toolbar({
                                height : 30,
                                items : []
                            });
							 if (isGranted("_FileAttachUp")) {
                                b.add(new Ext.Button({
                                        text : "上传文件",
                                        iconCls : "btn-add",
                                        handler : function () {
                                            
                                                    var e = this.centerPanel;
                                                    var f = Ext.getCmp("forder");
                                                    var g = f.selectedNode;
                                                    var h = 0;
                                                    if (g != null) {
                                                        h = g.id;
                                                    }
                                                    if (h == 0) {
                                                        Ext.ux.Toast.msg("操作信息","请从左选择文件夹");
                                                        return;
                                                    }
                                                    var a = new Ext.ux.UploadDialog.Dialog(
                                                            {
                                                                file_cat : this.file_cat,
                                                                url : __ctxPath+"/file-upload.do?fileDetail.forder.forId="+h,
                                                                scope : this,
                                                                callback : function(d) {
                                                                    if (d != null && d.length > 0) {
                                                                        var c = Ext.getCmp("fileDetailGrid");
                                                                        c.getStore().reload();
                                                                        a.close();
                                                                    }
                                                                    
                                                                }
                                                            }).show();
                                        }
                                    }));
                            }
							 if (isGranted("_FileAttachDel")) {
                                b.add(new Ext.Button({
                                        text : "删除",
                                        iconCls : "btn-delete",
                                        handler : function () {
                                            
                                                    var f = this.centerPanel;
                                                    var d = f
                                                            .getSelectionModel()
                                                            .getSelections();
                                                    if (d.length == 0) {
                                                        Ext.ux.Toast.msg("信息",
                                                                "请选择要删除的记录！");
                                                        return;
                                                    }
                                                    //删除grid 取ids
                                                    var ids = "";
                                                    for (var i = 0; i < d.length; i++) {
                                                        ids +=d[i].data.fileId;
                                                        if (i < d.length - 1) {
                                                            ids = ids + ",";
                                                        }
                                                    }
                                                    Ext.Msg
                                                            .confirm(
                                                                    "信息确认",
                                                                    "您确认要删除所选记录吗？",
                                                                    function(h) {
                                                                        if (h == "yes") {
                                                                            Ext.Ajax
                                                                                    .request({
                                                                                        url :__ctxPath+ "/system/deleteFileDetail.do",
                                                                                        params : {
                                                                                            ids : ids
                                                                                        },
                                                                                        method : "POST",
                                                                                        success : function(
                                                                                                i,
                                                                                                j) {
                                                                                            Ext.ux.Toast
                                                                                                    .msg(
                                                                                                            "操作信息",
                                                                                                            "成功删除文件！");
                                                                                            f
                                                                                                    .getStore()
                                                                                                    .reload();
                                                                                        },
                                                                                        failure : function(
                                                                                                i,
                                                                                                j) {
                                                                                            Ext.ux.Toast
                                                                                                    .msg(
                                                                                                            "操作信息",
                                                                                                            "操作出错，请联系管理员！");
                                                                                        }
                                                                                    });
                                                                        }
                                                                    });
                                        }
                                    }));
                            }
                            
						this.centerPanel = new Ext.grid.EditorGridPanel({
							region : "center",
							title : "文件列表",
							tbar : b,
							clicksToEdit : 1,
							id : "fileDetailGrid",
							store : this.store,
							sm : c,
							cm : a,
							height : 450,
							viewConfig : {
								forceFit : true,
								autoFill : true
							},
							bbar : new Ext.PagingToolbar({
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : "当前页记录索引{0}-{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							})
						});
					},
					onSearch : function(c) {
						var a = Ext.getCmp("fileDetailSearchForm");
						var b = Ext.getCmp("fileDetailGrid");
						if (a.getForm().isValid()) {
							var d = b.getStore();
							d.proxy = new Ext.data.HttpProxy({
								url:__ctxPath+ "/system/listFileDetail.do"
							});
							$search({
								searchPanel : a,
								gridPanel : b
							});
						}
					}
});
FileDetailSearchReset=function(){
	var a=Ext.getCmp("fileDetailSearchForm");
	a.getForm().reset();
}

FileAttachDetail.show1 = function(b){
    //先判断文件是否存在
//	window.location.href = __ctxPath + "/system/docToSwfFileDetail.do?fileId=" + b
	window.open(__ctxPath + "/system/docToSwfFileDetail.do?fileId=" + b);
}

FileAttachDetail.downLoad = function(b) {
	//先判断文件是否存在
	Ext.Ajax.request({
		url : __ctxPath+ "/system/fileExistsFileDetail.do?fileId="+b,
		method : "POST",
		success : function(i,j) {
			var result = Ext.decode(i.responseText);
			if(result.success==true){
				window.location = __ctxPath+"/file-download.do?fileId="+b;
			}else{
				/*Ext.MessageBox.show({
					title : "系统提示",
					msg : '服务器上资源不存在，请联系管理员！',
					buttons : Ext.MessageBox.OK,
					icon : "ext-mb-info"
				});*/
				Ext.ux.Toast.msg("提示信息","服务器上资源不存在，请联系管理员！");
				return;
			}
		},
		failure : function(i,j) {
			Ext.ux.Toast.msg("提示信息","操作出错，请联系管理员！");
		}
	});
	
};